package com.fuja.swf.ui;

// Author: Roberto Frota
// roberto@pdasoft.com.br
// GraphGen - Graph Generator
// 01/05/2004
// Import Packages
import waba.sys.*;
import waba.ui.*;
import waba.fx.*;
import waba.util.*;

/**
 * GraphGen = new GraphGen(); 
 * GraphGen.vec1=(text vector);
 * GraphGen.vec2=(percent vector); 
 *  
 * GraphGen.tipGra = 1; // Pie 
 * GraphGen.tipGra = 2; // Bar
 * 
 * popupBlockingModal(GraphGen);
 */
public class GraphGen extends Window {
	public static final int TYPE_PIE = 1;
	public static final int TYPE_BAR = 2;
	
	
	private Graphics g1;
	public Vector vec1, vec2;
	private Label label;
	private Vector colors;
	private Palette pallete;
	public int tipGra;
	private double fatorW, fatorH, fatorM;
	
	//INTERFACE
	private Button btnBack = new Button("Back");
	private Label lbInstructions = new Label("Click in the graph to see description");
		
	public GraphGen (){
		// Resize the window
		fatorH = (float) Settings.screenHeight / 160f;
		fatorW = (float) Settings.screenWidth / 160f;
		if (fatorH < fatorW)
			fatorM = fatorH;
		else
			fatorM = fatorW;

		// Label to show identifier
		add(label = new Label("               "),
				LEFT + (int) (1 + 2 * fatorW), BOTTOM - (int) (18 * fatorH));
		label.setRect(SAME, SAME, Settings.screenWidth, PREFERRED);

		// Graph Colors
		g1 = new Graphics(this);
		colors = new Vector(new Object[] { Color.getColor("FF6699"),
				Color.getColor("9966FF"), Color.getColor("FF9900"),
				Color.getColor("3399FF"), Color.getColor("FF0000"),
				Color.getColor("99CC33"), Color.getColor("800000"),
				Color.getColor("006699"), Color.getColor("3366FF"),
				Color.getColor("33CCFF"), Color.getColor("FF3399"),
				Color.getColor("800080"), Color.getColor("333366"),
				Color.getColor("669966"), Color.getColor("CC3366"),
				Color.getColor("33FFFF"), Color.getColor("FFCC99"),
				Color.getColor("666699"), Color.getColor("CC00CC"),
				Color.getColor("00FF99"), Color.getColor("FF6633"),
				Color.getColor("FFFF00"), Color.getColor("CC6600"),
				Color.getColor("FF0066"), Color.getColor("0099FF"),
				Color.getColor("33CC99"), Color.getColor("FFFF99"),
				Color.getColor("FFFF66"), Color.getColor("99FF99"),
				Color.getColor("FFFF33") });

		Color[] colorsArr = new Color[colors.size()];
		Vm.copyArray(colors.toObjectArray(), 0, colorsArr, 0, colors.size());
		pallete = new Palette(colorsArr);
		
		lbInstructions.setBackColor(Color.BLACK);
		lbInstructions.setForeColor(Color.WHITE);
		add(lbInstructions);
		lbInstructions.setRect(LEFT,TOP, Settings.screenWidth, PREFERRED);
		add(btnBack,RIGHT,AFTER + 1);
	}
	
	public final void onPaint(Graphics g) {
		super.onPaint(g);
		// Inicializers
		int tot = 0;
		int larg = 0;
		int alt = 0;
		int qtdReg = vec2.size();
		// double vlrMaiAbs = -1000000, vlrMai = -1000000, vlrMen = 1000000;
		double prcMai = 0;
		for (int i = 0; i < qtdReg; i++) {
			if (prcMai < Convert.toDouble(vec2.items[i].toString()))
				prcMai = Convert.toDouble(vec2.items[i].toString());
		}

		// Loop until 30 records
		for (int i = 0; i < qtdReg; i++) {
			if (i == 30)
				break;

			// Get the color
			g.setBackColor((Color) colors.items[i]);

			// Pie Graph
			if (tipGra == 1) {
				larg = (int) Math.round(3.6 * (Convert.toDouble(vec2.items[i]
						.toString())));
				g.fillPie((int) (80 * fatorW),
						(int) (45 * fatorM + 25 * fatorH),
						(int) (30 * fatorM + 25 * fatorH), tot, (tot + larg));
				tot += larg;
			}

			// Bar Graph
			if (tipGra == 2) {
				larg = (int) ((145 * fatorW) / qtdReg);
				if (i == 0)
					tot = (int) (8 * fatorW);
				alt = (int) ((105 * fatorH) * (Convert.toDouble(vec2.items[i]
						.toString()) / prcMai));
				if (alt < (int) (2 * fatorH))
					alt = (int) (2 * fatorH);
				g.fillRect(tot, (int) (125 * fatorH) - alt, larg, alt);
				tot += larg;
			}
		}

		g1 = g;
	}

	// Events
	public final void onEvent(Event event) {
		// Show text same color when tap a piece of Graph
		if ((event.target == btnBack) && (event.type==PenEvent.PEN_DOWN)){
			onBack();
		}else if ((event.type == PenEvent.PEN_UP) && (event.target != btnBack)) {
			PenEvent pV = (PenEvent) event;
			Color color = Color.getColor(g1.getPixel(pV.x, pV.y));
			int i = pallete.getColorIndex(color);
			if (i < vec1.size()) {
				label.setBackColor(color);
				String percent = vec2.items[i].toString();
				if (percent.length()>=5)
					percent = percent.substring(0,5);
				label.setText(percent + "% - "	+ vec1.items[i].toString());
				repaintNow();
			}
		}		
	}
	public void onBack(){}
}
